<?php
/*
 * 文件上传；
 */
function upload($path,$fileType,$maxSize=0,$upfile)
{
    $res =array(
        "info"=>"",
        "error"=>false,
        "newName"=>"",
    );
    if(empty($upfile)){
        $res['info']="文件不能为空";
        return $res;
    }
    if(!in_array($upfile['type'],$fileType)){
        $res['info']="文件信息有误";
        return $res;
    }
    if($upfile['size']<$maxSize&&$maxSize==0){
        $res['info']="超出文件可上传的大小";
        return $res;
    }else if($maxSize>0 && $upfile['size']>$maxSize){
        $res['info']="超出文件可上传的大小";
        return $res;
    }
    if($upfile['error']>0){
        switch($upfile['error']){
            case 1:
                $info ="上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值";
                break;
            case 2:
                $info = "表示上传文件大小超出了HTML表单隐藏域属性的MAX＿FILE＿SIZE元素所指定的最大值";
                break;
            case 3:
                $info = "表示文件只被部分上传";
                break;
            case 4:
                $info = "表示没有上传任何文件";
                break;
            case 6:
                $info = "找不到临时文件夹";
                break;
            case 7:
                $info = "文件写入失败";
                break;
        }
        $res['info'] = $info;
        $res['error'] = $upfile['error'];
    }
    //随机产生一个文件名；
    $pathinfo = pathinfo($upfile['name']);
    $ext = $pathinfo['extension'];
    $path = rtrim($path,"/")."/";
    do{
        $newName = date("YmdHis").rand(1000,9999).".".$ext ;
    }while(file_exists($path.$newName));
    if(is_uploaded_file($upfile['tmp_name'])){
        if(move_uploaded_file($upfile['tmp_name'],$path.$newName)){
            $res['info'] = "恭喜您文件上传成功，文件名是：".$newName;
            $res['error'] = true;
            $res['newName'] = $newName;
            return $res;
        }else{
            $res['info'] = "文件上传有误";
            return $res;
        }
    }else{
        $res['info'] = "不是上传文件";
        return $res;
    }
}
/*
 * 等比例缩放；
 */
function getImages($path,$pic,$line,$maxw,$maxh)
{
    $path = rtrim($path,"/")."/";
    $picName = getImagesize($path.$pic);
    //判断图片的类型是什么；
    switch($picName[2]){
        case 1:
            $im = imagecreatefromgif($path.$pic);
            break;
        case 2:
            $im = imagecreatefromjpeg($path.$pic);
            break;
        case 3:
            $im = imagecreatefrompng($path.$pic);
            break;
    }
    //获取源图片的宽高；
    $oldw = imagesx($im);
    $oldh = imagesy($im);
    if($oldw>$oldh){
        $bl = $oldw/$maxw;
        $logw = $oldw/$bl;
        $logh = $oldh/$bl;
    }else{
        $bl = $oldh/$maxh;
        $logw = $oldw/$bl;
        $logh = $oldh/$bl;
    }
    //把缩放的宽高，放到画布里面；
    $img = imagecreatetruecolor($logw,$logh);
    //合成图片；
    imagecopyresampled($img,$im,0,0,0,0,$logw,$logh,$oldw,$oldh);
    //判断用什么图片格式来输出；

    switch($picName[2]){
        case 1:
            imagegif($img,$path.$line.$pic);
            break;
        case 2:
            imagejpeg($img,$path.$line.$pic);
            break;
        case 3:
            imagepng($img,$path.$line.$pic);
            break;
    }
    //销毁图片；
    imagedestroy($im);
    imagedestroy($img);
//    echo "<script>alert('上传成功');window.location.href='ooo.php';</script>";
}